<?php
  class StuactivityController extends Controller{
           public $isActivated = 2;
    public function accessRules()
		 {
			return array(
				array(
					'allow',  // allow all users to access 'index' and 'view' actions.
					'expression'=>'$user->rights == 1',
				),
				array(
					'deny',  // deny all users
					'users'=>array('*'),
					'message'=>'You do not have access to this page ! ! ',
				),
			);
		 }
      public function actionIndex(){
		  $this->breadcrumbs=array(
               '学生活动'=>array('index'),
               '添加活动',
             );
		   $acs = Activity::model()->findAll('isclosed = 0');
		   $stuAcs  = Stuactivity::model()->findAll('uId = :u',array(':u'=>Yii::app()->user->getId()));
	       $this->render("index",array('acs'=>$acs,'stuAcs'=>$stuAcs));	    
		  
	  }
	  public function actionMyactivity(){
		   $this->breadcrumbs=array(
               '学生活动'=>array('index'),
               '我参加的活动',
             );
		   $stuAcs = Stuactivity::model()->findAll('uId = :u',array(':u'=>Yii::app()->user->getId()));
		   
	       $this->render("myactivity",array('stuAcs'=>$stuAcs));
		  
	  }
	  public function actionDelete(){
		  $request = Yii::app()->request;
		  if($request->isAjaxRequest){
			  $id = $request->getParam('id');
			  $stuAc = Stuactivity::model()->findByPk($id);
			  $ac = Activity::model()->findByPk($stuAc->activityId);
			  if($stuAc){
				  	   // condition  ---》 1 ---- 未审核 2 ---- 未通过 3 --- 已通过无法删除
				  if($stuAc->condition !== 3){
					  
					  $ac->attendnum -= 1; // 删除一条记录这个活动的参加人数减少 1 
					  if($stuAc->delete() && $ac->save()){
						  
						  echo json_encode(array('msg'=>'删除成功','code'=>200));
						  
					  }else{
						  
						  $this->logAr($stuAc);
						  echo json_encode(array('msg'=>'删除失败','code'=>501));
						  
					  }
				  }else{
					  
					    echo json_encode(array('msg'=>'无法删除','code'=>405));
					  
				  }
					  
				  
			  }else{
				  echo json_encode(array('msg'=>'没有找到这个条目','code'=>404));
				  
			  }
			  
		  }else{
			  $this->redirect('myactivity');
			  
		  }
		  
	  }
	  public function actionJoinactivity(){
		   
		   $request = Yii::app()->request;
		   $id = $request->getParam('id');
		   if(is_numeric($id)){
			   $ac = Activity::model()->findByPk($id);
			   if($ac){
				   // 只能参加未关闭的活动
				   if($ac->isclosed === '0'){
					    
						$stuAc = Stuactivity::model()->find('uId = :u and activityId = :a',array(':u'=>Yii::app()->user->getId(),':a'=>$ac->activityId));
						if(!$stuAc)
							$stuAc = new Stuactivity;
				   
				   }else{
					   
					   $this->redirect(array('stuactivity/index'));
				   }
				  
			   }else{
				   $this->redirect(array('stuactivity/index'));
			   }
			 
		   }else{
			   $this->redirect(array('stuactivity/index'));
		   }
	       
		   $this->render("joinactivity",array('ac'=>$ac,'stuAc'=>$stuAc));	  
	  }
	  public function actionDealjoin(){
		  $request = Yii::app()->request;
		  
		  if($request->requestType == 'POST'){
			
			  $activityId = $request->getparam('activityId');
			  $ac = Activity::model()->findByPk($activityId);
			if(!$ac)
				$this->redirect(array('activity/index'));
			$stuAc = Stuactivity::model()->find('uId = :u and activityId = :a',array(':u'=>Yii::app()->user->getId(),':a'=>$activityId));
			
			if($ac->stunum == $ac->attendnum && !$stuAc){
				
				Yii::app()->user->setFlash('join','人数已满'.'<br>'.'额定参加人数：'.$ac->stunum.'<br> 已参加人数：'.$ac->attendnum);
				    
				
			}else{
				if(!$stuAc)
				    $stuAc = new Stuactivity;
					$stuAc->uId = Yii::app()->user->getId();
					$stuAc->activityId = $ac->activityId;
					$stuAc->detail = $request->getParam('detail');
					$stuAc->addtime = date('Y-m-d H:i:s');
					$stuAc->condition = 1; // 1 ---- 未审核 2 ---- 未通过 3 --- 已通过
					
					if($stuAc->isNewRecord) // 新纪录才增加参加人数
					   $ac->attendnum += 1;
					if($stuAc->save()){
						
						if($ac->save()){
							Yii::app()->user->setFlash('join','添加/更新成功');
						}else{
							Yii::app()->user->setFlash('join','添加/更新失败');
						}
						
					}else{
						Yii::app()->user->setFlash('join','添加/更新失败');
					}
				
			}
			  
		  }else{
			  $this->redirect('index');
			  
		  }
		  $this->redirect(array('stuactivity/joinactivity','id'=>$activityId));
	  }
  }
  
  ?>